﻿#include "QCDialog.h"
#include "../Global/ResourcePath.h"
#include "../Global/ECGGlobal.h"

#include <QPixmap>
#include <QStyleOption>
#include <QPainter>
#include <QStyle>
#include <QPalette>
#include <QMovie>
#include <qnamespace.h>

#pragma execution_character_set("utf-8")

QCDialog::QCDialog(QWidget *parent)
    :QDialog(parent)
{
	m_bShowApply = false;
	m_bShowOK = true;
	m_bShowCancel = true;

    m_bDragging = false;
    m_nLastClickX = -1;
    m_nLastClickY = -1;

    this->setWindowFlags(Qt::FramelessWindowHint);
	headBackGround = new QWidget(this);
	headBackGround->setProperty("UIClass", "DialogHead");

    headIcon = new QLabel(this);
	QPixmap iconLogo = QPixmap(gProjectDir + gIconPath_Logo).scaled(32, 32, Qt::AspectRatioMode::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation);
	headIcon->setPixmap(iconLogo);
	headIcon->setStyleSheet("background:transparent; border: none;");

	headTitle = new QLabel(this);
    headTitle->setProperty("UIClass","DialogTitle");

    headCloseBtn = new QPushButton(this);
	QPixmap iconClose = QPixmap(gProjectDir + gIconPath_Cross).scaled(32, 32, Qt::AspectRatioMode::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation);
	headCloseBtn->setProperty("UIClass", "Icon");
	headCloseBtn->setIcon(iconClose);
	headCloseBtn->setIconSize(QSize(32, 32));
	headCloseBtn->setFlat(true);
    connect(headCloseBtn,SIGNAL(clicked()),this,SLOT(onClose()));

	m_pWidgetBKGFooter = new QWidget(this);
	m_pWidgetBKGFooter->setProperty("UIClass", "DialogFoot");

	m_pBtnApply = new QPushButton(m_pWidgetBKGFooter);
	m_pBtnApply->setText("应用");
	m_pBtnApply->setVisible(false);
	connect(m_pBtnApply, SIGNAL(clicked()), this, SLOT(onApply()));

	m_pBtnOK = new QPushButton(m_pWidgetBKGFooter);
	m_pBtnOK->setText("确定");
	m_pBtnOK->setVisible(false);
	connect(m_pBtnOK, SIGNAL(clicked()), this, SLOT(onOK()));

	m_pBtnCancel = new QPushButton(m_pWidgetBKGFooter);
	m_pBtnCancel->setText("取消");
	m_pBtnCancel->setVisible(false);
	connect(m_pBtnCancel, SIGNAL(clicked()), this, SLOT(onCancel()));

	m_pWidgetBKG = new QWidget(this);
	m_pWidgetBKG->setProperty("UIClass", "DialogBKG");


	mainWnd = NULL;
}

QCDialog::~QCDialog()
{

}

void QCDialog::onClose()
{
    this->close();
}

void QCDialog::onApply()
{
}

void QCDialog::onOK()
{
	this->accept();
}

void QCDialog::onCancel()
{
	this->reject();
}

void QCDialog::paintEvent(QPaintEvent *e)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(e);
}

void QCDialog::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDragging){
        QPoint  ptMove = event->globalPos();
        this->move(this->pos()+ptMove-m_ptPress);
        m_ptPress = ptMove;
    }
    event->accept();
}

void QCDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton &&
        this->headBackGround->geometry().contains(event->globalPos()-this->frameGeometry().topLeft())){
        m_ptPress = event->globalPos();
        m_bDragging = true;
    }
	event->accept();
}

void QCDialog::mouseReleaseEvent(QMouseEvent *event)
{
	m_bDragging = false;
	event->accept();
}

void QCDialog::setSize(int w, int h)
{
    m_nWidth = w;
    m_nHeight = h;
}

void QCDialog::setQCGeometry(int x, int y, int w, int h)
{
    this->setGeometry(x,y,w,h);

	headBackGround->setGeometry(0, 0, w, QCDIALOG_HEAD_HEIGHT);
	headIcon->setGeometry(10, 10, 32, 32);
    headIcon->show();

	headTitle->adjustSize();
	QSize sizeTitle = headTitle->size();
	headTitle->setGeometry(52, (QCDIALOG_HEAD_HEIGHT - sizeTitle.height()) / 2, sizeTitle.width(), sizeTitle.height());

	headCloseBtn->setGeometry(w - 32 - 20, 10, 32, 32);

	m_pWidgetBKGFooter->setGeometry(0, h - QCDIALOG_FOOT_HEIGHT, w, QCDIALOG_FOOT_HEIGHT);

	int margin_vertical = 8;
	int space_horizon = 20;
	int height_btn = QCDIALOG_FOOT_HEIGHT - margin_vertical * 2;
	int width_btn = height_btn * 2.4;

	int num_button = 3;
	if (!m_bShowOK) num_button--;
	if (!m_bShowApply) num_button--;
	if (!m_bShowCancel) num_button--;

	if (num_button == 1)
		width_btn *= 2;

	int x_btn = (w - width_btn * num_button - space_horizon * (num_button - 1)) / 2;
	int y_btn = margin_vertical;

	if (m_bShowApply){
		m_pBtnApply->setVisible(true);
		m_pBtnApply->setGeometry(x_btn, y_btn, width_btn, height_btn);
		x_btn += width_btn + space_horizon;
	}

	if (m_bShowOK){
		m_pBtnOK->setVisible(true);
		m_pBtnOK->setGeometry(x_btn, y_btn, width_btn, height_btn);
		x_btn += width_btn + space_horizon;
	}

	if (m_bShowCancel){
		m_pBtnCancel->setVisible(true);
		m_pBtnCancel->setGeometry(x_btn, y_btn, width_btn, height_btn);
	}

	int footHeight = num_button == 0 ? 0 : QCDIALOG_FOOT_HEIGHT;
	m_pWidgetBKG->setGeometry(1, QCDIALOG_HEAD_HEIGHT, w - 2, h - QCDIALOG_HEAD_HEIGHT - QCDIALOG_FOOT_HEIGHT);
}

void QCDialog::setMainWnd(QWidget* pWidget)
{
	mainWnd = pWidget;
}

void QCDialog::setQCTitle(const QString &s)
{
    headTitle->setText(s);
}

void QCDialog::moveCenter(int screenWidth, int screenHeight)
{
    int startX = (screenWidth-m_nWidth)/2;
    int startY = (screenHeight-m_nHeight)/2;
    this->setQCGeometry(startX,startY,m_nWidth,m_nHeight);
}

void QCDialog::resetStyle()
{
	headTitle->setStyleSheet("font-size:16px;");
}

void QCDialog::setShowApply(bool bShow)
{
	m_bShowApply = bShow;
}

void QCDialog::setShowOK(bool bShow)
{
	m_bShowOK = bShow;
}

void QCDialog::setShowCancel(bool bShow)
{
	m_bShowCancel = bShow;
}
